package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.StyleWaterProfiles;

public class StyleWaterProfilesPresenter extends AbstractPresenter<StyleWaterProfiles> {
	final String SINGLEITEM = "styleWaterProfiles";
	final String MULTIITEM = "styleWaterProfiless";
	
	public ArrayList<StyleWaterProfiles> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public StyleWaterProfiles tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public StyleWaterProfiles tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			StyleWaterProfiles item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public StyleWaterProfiles createItemFromJSONObject(JSONObject jsonObject){
		StyleWaterProfiles item = new StyleWaterProfiles();
		try{
			item.setCa(jsonObject.getDouble("ca"));
			item.setCityName(jsonObject.getString("cityName"));
			item.setCl(jsonObject.getDouble("cl"));
			item.setCo3(jsonObject.getDouble("co3"));
			item.setCountryName(jsonObject.getString("countryName"));
			item.setId(jsonObject.getInt("profileID"));
			item.setMg(jsonObject.getDouble("mg"));
			item.setNa(jsonObject.getDouble("na"));
			item.setpH(jsonObject.getDouble("PH"));
			item.setSo4(jsonObject.getDouble("so4"));
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(StyleWaterProfiles item){
		try{
			JSONObject json = new JSONObject();
			json.put("ca", item.getCa());
			json.put("cityName", item.getCityName());
			json.put("cl", item.getCl());
			json.put("co3", item.getCo3());
			json.put("countryName", item.getCountryName());
			json.put("profileID", item.getId());
			json.put("mg", item.getMg());
			json.put("na", item.getNa());
			json.put("PH", item.getpH());
			json.put("so4", item.getSo4());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public StyleWaterProfiles getEmptyEntity(){
		StyleWaterProfiles entity = new StyleWaterProfiles();
		return entity;
	}
}
